package com.wehealth.electrocardiograph_two_client.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;


import com.wehealth.electrocardiograph_two_client.R;
import com.wehealth.electrocardiograph_two_client.db.PatientUserDao;

import java.util.ArrayList;
import java.util.List;

public class PatientUserAdapter extends BaseAdapter {

    private Context context;
    private LayoutInflater inflater;
    private List<String[]> list;
    private int DataType = 0;

    public PatientUserAdapter(Context ctx, int type) {
        context = ctx;
        inflater = LayoutInflater.from(context);
        list = new ArrayList<>();
        DataType = type;
    }

    public void addLists(List<String[]> lists){
        this.list.clear();
        this.list.addAll(lists);
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (list==null) {
            return 0;
        }
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("InflateParams") @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder vh;
        if (convertView == null) {
            vh = new ViewHolder();
            convertView = inflater.inflate(R.layout.patientuser_popwitem, null);
            vh.idTV = convertView.findViewById(R.id.patientuser_item_pid);
            vh.nameTV = convertView.findViewById(R.id.patientuser_item_pname);
            vh.deleteBtn = convertView.findViewById(R.id.patientuser_item_delete);
            convertView.setTag(vh);
        }else {
            vh = (ViewHolder) convertView.getTag();
        }
        if (position%2==0){
            convertView.setBackgroundColor(context.getResources().getColor(R.color.text_white));
        }else{
            convertView.setBackgroundColor(context.getResources().getColor(R.color.error_item_color));
        }
        if (DataType==0){
            vh.idTV.setText(list.get(position)[0]);
            vh.nameTV.setVisibility(View.GONE);
        }else {
            vh.nameTV.setVisibility(View.VISIBLE);
            vh.idTV.setText(list.get(position)[0]);
            vh.nameTV.setText(list.get(position)[1]);
        }
        vh.deleteBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                PatientUserDao.getInstance().deletePatientUserById(list.get(position)[0]);
                list.remove(position);
                notifyDataSetChanged();
            }
        });
        return convertView;
    }

    class ViewHolder{
        TextView idTV;
        TextView nameTV;
        Button deleteBtn;
    }

}